//
//  LinePointButton.m
//  BezierPathDraw
//
//  Created by huyuchen on 2018/3/8.
//  Copyright © 2018年 Hyc. All rights reserved.
//

#import "LinePointButton.h"

@interface LinePointButton ()
@property (nonatomic, strong) UIImageView *selectImage;
@end
@implementation LinePointButton

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat imageWH = 10.f;
    CGFloat imageY = 5.f;
    CGFloat imageX = (self.frame.size.width-imageWH)*0.5;
    self.imageView.frame = CGRectMake(imageX, imageY, imageWH, imageWH);
    
    CGFloat titleW = self.frame.size.width;
    CGFloat titleY = CGRectGetMaxY(self.imageView.frame)+5;
    CGFloat titleH = self.frame.size.height - titleY;
    CGFloat titleX = 0;
    
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);

}


+ (instancetype)linePointStopButtonWithTitle:(NSString *)title {
    LinePointButton *button = [LinePointButton buttonWithType:UIButtonTypeCustom];
    button.type = LinePointDetailTypeStop;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:10.f];
    [button setImage:ImageNamed_bb(@"app_circuit_section_img") forState:UIControlStateNormal];

    return button;
}

+ (instancetype)linePointSectionButtonWithTitle:(NSString *)title style:(LinePointDetailType)style {
    LinePointButton *button = [LinePointButton buttonWithType:UIButtonTypeCustom];
    button.type = style;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:10.f];
    if (style == LinePointDetailTypeStart) {
        [button setImage:ImageNamed_bb(@"app_circuit_start_station_img") forState:UIControlStateNormal];
    }
    else if (style == LinePointDetailTypeStop) {
        [button setImage:ImageNamed_bb(@"app_circuit_station_img") forState:UIControlStateNormal];
    }
    else if (style == LinePointDetailTypeSection) {
        [button setImage:ImageNamed_bb(@"app_circuit_section_img") forState:UIControlStateNormal];
    }
    else if (style == LinePointDetailTypeEnd || style == LinePointDetailTypeSectionEnd) {
        [button setImage:ImageNamed_bb(@"app_circuit_end_station_img") forState:UIControlStateNormal];
    }
    [button setTitleColor:Color_Hex(0x565656) forState:UIControlStateNormal];
    return button;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.numberOfLines = 0;
        self.selectImage = [[UIImageView alloc] initWithImage:ImageNamed_bb(@"app_select_station_ic")];
        [self addSubview:self.self.selectImage];
        self.selectImage.hidden = YES;
        [self.selectImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.mas_equalTo(-13);
            make.width.height.mas_equalTo(13);
        }];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (void)setIsSelect:(BOOL)isSelect {
    _isSelect = isSelect;
    self.selectImage.hidden = !isSelect;
}

//- (CGRect)titleRectForContentRect:(CGRect)contentRect {
//    return CGRectMake(0, contentRect.size.height-20, contentRect.size.width, 20);
//}
//
//- (CGRect)imageRectForContentRect:(CGRect)contentRect {
//    return CGRectMake((contentRect.size.width-10)*0.5, 20, 10, 10);
//}

- (void)setLineColor:(UIColor *)lineColor {
    _lineColor = lineColor;
    self.imageView.tintColor = lineColor;
}
@end
